//
//  MTStateButton.swift
//  OneMeeting
//
//  Created by zhangheng on 2019/8/9.
//  Copyright © 2019 www.zhangheng.com. All rights reserved.
//

import Foundation
import AppKit


class BaseButton: NSButton {

    var bgNormal:NSColor?
    var bgHighlighted:NSColor?
    var bgSelected:NSColor?
    
    var mouseTrackingArea:NSTrackingArea?
   
    
    var boardW_noml:CGFloat? {
        didSet {
            self.layer?.borderWidth = boardW_high!
        }
    }
    
    var boardW_high:CGFloat? {
        didSet {
            self.layer?.borderWidth = boardW_high!
        }
    }
    
    override init(frame frameRect: NSRect) {
        super.init(frame: frameRect)
        
        self.isBordered = false
        self.setButtonType(NSButton.ButtonType.momentaryChange)
        
        self.wantsLayer = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open func boardColor(normal:NSColor,highlighted:NSColor?,selected:NSColor?) -> () {
        
        self.isBordered = false
        self.setButtonType(NSButton.ButtonType.momentaryChange)
        
        self.bgNormal = normal
        self.bgHighlighted = highlighted
        self.bgSelected = selected
        
        self.layer?.backgroundColor = normal.cgColor
        self.wantsLayer = true
    }
    
   
    
    open var hover:Bool = false{
        didSet {
            if self.bgHighlighted != nil && self.bgNormal != nil {
                self.layer?.backgroundColor = (hover ? self.bgHighlighted:self.bgNormal)!.cgColor
            }
            
        }
    }
    
    open var active:Bool = false{
        didSet {
            
        }
    }
    
    ///////////复写
    override func updateTrackingAreas() {
        super.updateTrackingAreas()
        
        if self.mouseTrackingArea != nil {
            self.removeTrackingArea(self.mouseTrackingArea!)
        }
        
        self.mouseTrackingArea = NSTrackingArea.init(rect: self.bounds, options: [NSTrackingArea.Options.mouseEnteredAndExited,NSTrackingArea.Options.activeAlways], owner: self, userInfo: nil)
        self.addTrackingArea(self.mouseTrackingArea!)
    }
    
    override func mouseEntered(with event: NSEvent) {
        super.mouseEntered(with: event)
        self.hover = true
    }
    
    override func mouseExited(with event: NSEvent) {
        super.mouseExited(with: event)
        self.hover = false
    }
    
    override var acceptsFirstResponder: Bool {
        return false
    }
    
    
}
